<?php
/**
 * Created by PhpStorm.
 * User: ZhengYaFeng
 * Date: 2016/10/19
 * Time: 14:36
 */
namespace App\Http\Controllers\Admin\AdminUser;

use App\Http\Controllers\Admin\Index\AdminiController;
use App\Http\Requests\StoreAdminiUserNode;
use Illuminate\Support\Facades\DB;

class AdminiUserNodeController extends AdminiController
{
    /**
     * 权限节点列表页...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:权限节点列表页>
     * @return mixed
     */
    public function index()
    {
        $data = array();
        $data = $this->getList('index',$data);
        return \Helpers::showViewCURD('Admin.AdminUserNode.index',3,'',$data);
    }

    /**
     * 获取列表页数据...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:获取列表页数据>
     * @param string $flag
     * @param $data
     * @return mixed
     */
    public function getList($flag='index',$data)
    {
        $selectId = $flag == 'rb' ? 2 : 1;
        //获取父级节点
        $res = DB::select('
                        select a.name,a.id,a.pid,a.node_name,a.summary,a.is_show,b.name as pidname,concat(a.path,",",a.id) as bpath
                        from wj_admini_node a 
                        left join wj_admini_node b 
                        on a.pid = b.id 
                        where a.is_del = ? 
                        order by bpath,a.sort
                ',[$selectId]);
        //生成公共处理前格式
        $data['list'] = empty($res) ? '' : json_encode($res);
        //公共处理数据给页面
        $data['list'] = json_decode($data['list'],true);
        return $data;
    }

    /**
     * 权限节点新增页...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:权限节点新增页>
     * @return mixed
     */
    public function create()
    {
        $data = array();
        $data = $this->getCreateOrEditD($data, 'arr');
        return \Helpers::showViewCURD('Admin.AdminUserNode.create',1,'新增',$data);
    }

    /**
     * 获取修改新增公共数据...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:获取修改新增公共数据>
     * @param $data
     * @param string $strOrArr
     * @return mixed
     */
    public function getCreateOrEditD($data,$strOrArr='str')
    {
        //获取所属父级别字典
        $where = array('is_del'=>array('sb'=>'=','v'=>1));
        $pidListRes =\Helpers::DbForTree('admini_node','','id,pid,path,name,concat(path,\',\',id) as bpath',$where,'bpath');
        //生成所属父级栏目下拉字符串
        $data['pidListStr'] = \Helpers::createTreeSelect($pidListRes,$strOrArr,array('id'=>'id','pid'=>'pid','path'=>'path','title'=>'name'));
        return $data;
    }

    /**
     * 权限节点新增操作...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:权限节点新增操作>
     * @param StoreAdminiUserNode $request
     * @return $this
     */
    public function store(StoreAdminiUserNode $request)
    {
        //验证post提交
        if($request->isMethod('post'))
        {
            $post = $request->all();
            $inserArr = $this->addOrEditFormatD($post);
            //入库
            $res = DB::table('admini_node')->insertGetId($inserArr);
            $errors = $res ? '新增成功!' : '新增失败!';
            $isSuccess = $res ? 'success' : 'error';
            \Helpers::insertUserActionLog('rbacNode','add',$res,$isSuccess,$inserArr);
            return redirect()->back()->withErrors($errors);
        }else{
            abort(404);
        }
    }

    /**
     * 公共格式化新增修改数据...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:公共格式化新增修改数据>
     * @param $post
     * @param string $isadd
     * @return array
     */
    public function addOrEditFormatD($post,$isadd='add')
    {
        $arr = array();
        $arr['name'] = $post['name'];
        $arr['node_name'] = $post['node_name'];
        $pid = explode('_',$post['pid']);
        $arr['pid'] = !empty($pid[0]) ? $pid[0] : 0;
        $arr['path'] = !empty($pid[0]) && $pid[1] != '' ? $pid[1].','.$pid[0] : 0;
        $arr['summary'] = !empty($post['summary']) ? $post['summary'] : '';
        $arr['is_show'] = $post['is_show'];
        $arr['sort'] = $post['sort'];
        $key = $isadd == 'add' ? 'create_time' : 'edit_time';
        $arr[$key] = time();
        $admin_id = json_decode(session('Z_AdminiUser'),true);
        $arr['admin_id'] = !empty($admin_id['id']) ? $admin_id['id'] : '';
        $arr['is_del'] = 1;
        return $arr;
    }

    /**
     * 修改页面...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:修改页面>
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        //验证数据
        if(empty($id)) abort(404);
        $data = array();
        $data = $this->getCreateOrEditD($data,'arr');
        $data['editList'] = DB::table('admini_node')->where('id',$id)->first();
        return \Helpers::showViewCURD('Admin.AdminUserNode.create',2,'修改',$data);
    }

    /**
     * 修改操作...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:>
     * @param $id
     * @param StoreAdminiUserNode $request
     * @return $this
     */
    public function update($id,StoreAdminiUserNode $request)
    {
        if(empty($id)) abort(404);
        //修改数据处理
        $editArr = $this->addOrEditFormatD($request,'edit');
        $res = DB::table('admini_node')
            ->where('id',intval($id))
            ->update($editArr);
        //根据入库结果返回状态
        $errors = $res ? '修改成功!' : '修改失败!';
        $isSuccess = $res ? 'success' : 'error';
        \Helpers::insertUserActionLog('rbacNode','up',$id,$isSuccess,$editArr);
        return redirect()->back()->withErrors($errors);
    }

    /**
     * 删除操作...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:删除操作>
     * @param $id
     * @return string
     */
    public function del($id)
    {
        //验证传参,ac 1.逻辑删除 2.物理删除 3.还原
        if(empty($id) || empty($_POST['ac'])) abort(404);
        $status = $_POST['ac'];
        //逻辑删除或者物理删除验证是否存在下级,无下级可执行下一步
        //检测当前数据是否存在下级,有下级必须删除下级后才能删除上级
        $hasChildren = DB::table('admini_node')->where('is_del',$status)->where('pid',$id)->first();
        if($hasChildren) die(json_encode(array('error'=>-1,'msg'=>'当前数据存在子集分类,请删除后再试!')));
        //status:1.逻辑删除数据,2.物理删除数据
        if($status == 1) $res = DB::table('admini_node')->where('id',intval($id))->update(array('is_del'=>2));
        if($status == 2) $res = DB::table('admini_node')->where('id',intval($id))->delete();
        $preMsg = '删除';
        $errorCode = $res ? 1 : -1;
        $msg = $res ? $preMsg.'成功!' : $preMsg.'失败!';
        $isSuccess = $res ? 'success' : 'error';
        \Helpers::insertUserActionLog('rbacNode','del',$id,$isSuccess,$_POST);
        return json_encode(array('error'=>$errorCode,'msg'=>$msg));
    }

    /**
     * 还原数据...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:还原数据>
     * @param $id
     * @return string
     */
    public function back($id)
    {

        //验证传参,ac 1.逻辑删除 2.物理删除 3.还原
        if(empty($id) || empty($_POST['ac'])) abort(404);
        //还原逻辑删除数据
        $res = DB::table('admini_node')->where('id',intval($id))->update(array('is_del'=>1));
        $preMsg = '还原';
        $errorCode = $res ? 1 : -1;
        $msg = $res ? $preMsg.'成功!' : $preMsg.'失败!';
        $isSuccess = $res ? 'success' : 'error';
        \Helpers::insertUserActionLog('rbacNode','back',$id,$isSuccess,$_POST);
        return json_encode(array('error'=>$errorCode,'msg'=>$msg));
    }

    /**
     * 回收站...
     * @author  <ZhengYaFeng>
     * @data    <Y-m-d H:i:s>
     * @access  <public>
     * @return  <view>
     * @des     <描述:回收站>
     * @return mixed
     */
    public function rb()
    {
        $data = array();
        $data = $this->getList('rb',$data);
        return \Helpers::showViewCURD('Admin.AdminUserNode.index',4,'',$data);
    }
}